package mi.payment.utils.transaction;

import android.content.Context;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 扫码支付退货交易
 *
 * @author tomcat on 2017/2/17.
 */
public class QRCodeReturnGoodsTask extends TransactionTask<TradeData, Void, TradeData> {

    protected QRCodeReturnGoodsTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        setDBStorage(true);
        setUpdateSettlementTable(false);
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_QR_CODE_RETURN_GOODS);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("200000");
        dbBean.setTransType("25");
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);

        // fix bug:如果输入的订单号不足12位补0
        String referNo;
        if (dbBean.thirdTransNum.length() < 12) {
            referNo = String.format(Locale.getDefault(), "%012d", Integer.parseInt(dbBean.thirdTransNum));
        } else if (dbBean.thirdTransNum.length() > 12) {
            referNo = dbBean.thirdTransNum.substring(0, 12);
        } else {
            referNo = dbBean.thirdTransNum;
        }
        dbBean.originalReferNo = referNo;

        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 031:反扫 032:正扫
        String value22 = "031";
        if (dbBean.qrCodeScanModel == 1) {
            value22 = "032";
        } else if (dbBean.qrCodeScanModel == 2) {
            value22 = "031";
        }
        // 填写22域
        MsgField fieldF022 = map.get("F022");
        fieldF022.setDataStr(value22);
        fieldF022.setDataLength(value22.length());

        // 通道类型 微信-TX01  盛付通钱包-SFT01  支付宝-ZFB01
        String channelType = "TX01";
        if (dbBean.transPlatform == 1) {
            channelType = "ZFB01";
        } else if (dbBean.transPlatform == 2) {
            channelType = "TX01";
        } else if (dbBean.transPlatform == 4) {
            channelType = "CUP01";
        }

        // 填写47域
        MsgField fieldF047 = map.get("F047");
        StringBuilder value47 = new StringBuilder();
        value47.append("TXNWAY=");
        value47.append(channelType);
        fieldF047.setDataStr(value47.toString());
        fieldF047.setDataLength(value47.length());

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        String value60 = dbBean.getTransType() + dbBean.getBathNo();
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 填写61域
        MsgField fieldF061 = map.get("F061");
        // 原批次，原流水，原交易日期
        String value61;
        if (TextUtils.isEmpty(dbBean.getOriginalPOSNum())) {
            value61 = "000000000000" + dbBean.getOriginalTransDate();
        } else {
            value61 = dbBean.getBathNo() + dbBean.getOriginalPOSNum() + dbBean.getOriginalTransDate();
        }


//        String value61 = "000000"; // +"000000"+"0000";
        fieldF061.setDataStr(value61);
        fieldF061.setDataLength(value61.length());

        // 填写63域
        MsgField fieldF063 = map.get("F063");
        fieldF063.setDataStr("000");
        fieldF063.setDataLength(3);

        // 添加自定义的商户号
        map = ParseUtils.addCustomTransID(map);

        return EncodePackage.assembly(map, "0220", TerminalSecure.PROCESSREQUEST_NONE);
    }

    @Override
    protected void onUnpacked(HashMap<String, MsgField> resp) {
        if ("00".equals(resultStr)) {
            // 撤销成功
            dbBean.QRCodeTransState = 1;
        } else {
            // 撤销失败
            dbBean.QRCodeTransState = -1;
        }
    }
}
